import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;

/**
 * Title:  Group.java
 * Description:  This class offers a basic GUI
 * Copyright:    Copyright (c) 2009
 * @author LI Shangru   annotated by WANG Jiechao
 * @version 1.0
 */

public class tabs extends JPanel implements ActionListener{
	JButton btn1,btn2;//two buttons in the left and right
	JTabbedPane display;//A component that lets the user switch between a group of components by clicking on a tab with a given title and/or icon
        private JPopupMenu popup;//a menu appears when users right click
        private JMenuItem menuNew, menuClose;//two items in popup
	public tabs(){
		  
		   display=new JTabbedPane();
		   this.setLayout(new BorderLayout());
		   display.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);//when there are too many tabs to be shown in one run,
                                                                     //users can scroll and see the unshown ones
	          this.add(display,BorderLayout.CENTER);//add them
              this.add(new JButton(),BorderLayout.WEST);
              this.add(new JButton(),BorderLayout.EAST);

               popup=new JPopupMenu();
               menuNew = new JMenuItem("new");//initialize two items in popup
               menuClose= new JMenuItem("close");
               menuNew.addActionListener(this);
               menuClose.addActionListener(this);
               popup.add(menuNew);//add them and separators
               popup.addSeparator();
               popup.add(menuClose);
               popup.addSeparator();
               display.addMouseListener(new PopupListener());//add listener
	}

         class PopupListener extends MouseAdapter {
        	public void mousePressed(MouseEvent e) {
        		if(e.getButton() == MouseEvent.BUTTON3){//when user right click (BUTTON3 means right click in this context)
        			popup.show((e.getComponent()), e.getX(), e.getY());//create a popup at the position where the mouse click	    
        		}
            	}
        }

		@Override
		public void actionPerformed(ActionEvent e) {
			int tabnumber=display.getSelectedIndex();
			if(e.getSource() == menuNew){//if item "New" has been clicked
				String str=JOptionPane.showInputDialog("code");//a dialog shows up asking for code and store user input in "str"     				
				boolean wrongCode = false;
				for (int i =0; i<str.length();i++){
					if (!Character.isDigit(str.charAt(i))){  //if any character is not digit
						System.out.println("no such a code");//then this code is unacceptable
						wrongCode = true;
						
			 	            }

				}

				if (!wrongCode){//if this code is acceptable 
				int code=Integer.parseInt(str);//convert it to integer	
				display.addTab(str, new content());//add this tab with code in "str"
				}
				else {JOptionPane.showMessageDialog(null, "no such code", "alert", JOptionPane.ERROR_MESSAGE);}//inform the user this is not a valid code

			}
			if(e.getSource()==menuClose){
				 // if the user select to quit the program
			      display.remove(tabnumber);//remove the selected one
			      
			    
				
			}
			
		}
		
	}
